// import React, { ReactNode, useCallback, useState } from 'react';
// import * as auth from 'auth-provider';
// import { User } from 'screens/project-list/search-panel';
// import { http } from 'utils/http';
// import { useMount } from '../utils';
// import { useAsync } from '../utils/use-async';
// import { FullPageErrorFallback, FullPageLoading } from '../components/lib';
//
// import * as authStore from 'store/auth.slice';
// import { useAppDispatch, useAppSelector } from '../store';
//
// export interface AuthForm {
//   username: string;
//   password: string;
// }
//
// export const bootstrapUser = async () => {
//   let user = null;
//   const token = auth.getToken();
//   if (token) {
//     const data = await http('me', { token });
//     user = data.user;
//   }
//   return user;
// };
//
// // const AuthContext = React.createContext<
// //   | {
// //       user: User | null;
// //       register: (form: AuthForm) => Promise<void>;
// //       login: (form: AuthForm) => Promise<void>;
// //       logout: () => Promise<void>;
// //     }
// //   | undefined
// // >(undefined);
// // AuthContext.displayName = 'AuthContext';
//
// export const AuthProvider = ({ children }: { children: ReactNode }) => {
//   const { error, isLoading, isIdle, isError, run } = useAsync<User | null>();
//   const dispatch: (...args: unknown[]) => Promise<User> = useAppDispatch();
//   //
//   useMount(() => {
//     run(dispatch(authStore.bootstrap()));
//   });
//
//   if (isIdle || isLoading) {
//     return <FullPageLoading />;
//   }
//
//   if (isError) {
//     return <FullPageErrorFallback error={error} />;
//   }
//   return <div>{children}</div>;
// };
//
// export const useAuth = () => {
//   const dispatch = useAppDispatch();
//   const user = useAppSelector(authStore.selectUser);
//   const login = useCallback((form: AuthForm) => dispatch(authStore.login(form)), [dispatch]);
//   const register = useCallback((form: AuthForm) => dispatch(authStore.register(form)), [dispatch]);
//   const logout = useCallback(() => dispatch(authStore.logout()), [dispatch]);
//
//   return {
//     user,
//     login,
//     register,
//     logout,
//   };
// };
